package com.nf.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf.entity.Student;

import java.io.IOException;
import java.io.PrintWriter;

@javax.servlet.annotation.WebServlet("/TestAction")
public class TestAction extends javax.servlet.http.HttpServlet {

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {


    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "application/json;charset=UTF-8");

        Student s = new Student();
        s.setUserName("张学友");
        s.setSex(null);
        //响应客户端
        ObjectMapper objectMapper = new ObjectMapper();
        String json_str = objectMapper.writeValueAsString(s);
        //String json_str = "{\"userName\":\""+s.getUserName()+"\",\"sex\":\""+s.getSex()+"\"}";
        response.getWriter().print(json_str);

        //request.setAttribute("kkk",s);//model.addAttribute("kkk",s);

        //模拟视图解析器推送请求到jsp
        //request.getRequestDispatcher("test.jsp").forward(request,response);

    }
}
